package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.ProductReviewDTO;
import com.zenithmind.mall.pojo.query.ProductReviewQuery;
import com.zenithmind.mall.pojo.vo.ProductReviewVO;
import com.zenithmind.mall.pojo.vo.ReviewStatisticsVO;
import com.zenithmind.mall.service.ProductReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品评价控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/review")
@RequiredArgsConstructor
@Tag(name = "商品评价管理", description = "商品评价相关接口")
public class ProductReviewController {

    private final ProductReviewService reviewService;

    @GetMapping("/page")
    @Operation(summary = "分页查询商品评价", description = "分页查询商品评价列表")
    @RequireAuth(admin = true)
    public Result<PageResult<ProductReviewVO>> getReviewPage(@Validated ProductReviewQuery query) {
        log.info("分页查询商品评价，查询条件: {}", query);
        PageResult<ProductReviewVO> result = reviewService.getReviewPage(query);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取评价详情", description = "根据评价ID获取详细信息")
    @Parameter(name = "id", description = "评价ID", required = true)
    public Result<ProductReviewVO> getReviewById(@PathVariable String id) {
        log.info("获取评价详情: {}", id);
        ProductReviewVO result = reviewService.getReviewById(id);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建商品评价", description = "用户创建商品评价")
    @RequireAuth
    @Log(description = "创建商品评价", operationType = OperationTypeEnum.ADD)
    public Result<String> createReview(@Validated @RequestBody ProductReviewDTO reviewDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 创建商品评价: {}", userId, reviewDTO.getProductId());
        String reviewId = reviewService.createReview(reviewDTO, userId);
        return Result.success(reviewId);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新商品评价", description = "更新商品评价信息")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth
    @Log(description = "更新商品评价", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateReview(@PathVariable String id, 
                                      @Validated @RequestBody ProductReviewDTO reviewDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 更新商品评价: {}", userId, id);
        Boolean result = reviewService.updateReview(id, reviewDTO, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品评价", description = "删除商品评价")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth
    @Log(description = "删除商品评价", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteReview(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 删除商品评价: {}", userId, id);
        Boolean result = reviewService.deleteReview(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除商品评价", description = "批量删除商品评价")
    @RequireAuth(admin = true)
    @Log(description = "批量删除商品评价", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteReviewsBatch(@RequestBody List<String> ids) {
        log.info("批量删除商品评价，数量: {}", ids.size());
        Boolean result = reviewService.deleteReviewsBatch(ids);
        return Result.success(result);
    }

    @GetMapping("/product/{productId}")
    @Operation(summary = "获取商品评价", description = "获取指定商品的评价列表")
    @Parameter(name = "productId", description = "商品ID", required = true)
    public Result<PageResult<ProductReviewVO>> getProductReviews(
            @PathVariable String productId,
            @RequestParam(required = false) Integer rating,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取商品评价: {}, 评分: {}", productId, rating);
        PageResult<ProductReviewVO> result = reviewService.getProductReviews(productId, rating, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户评价", description = "获取当前用户的评价列表")
    @RequireAuth
    public Result<PageResult<ProductReviewVO>> getUserReviews(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 获取评价列表", userId);
        PageResult<ProductReviewVO> result = reviewService.getUserReviews(userId, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/statistics/{productId}")
    @Operation(summary = "获取商品评价统计", description = "获取商品的评价统计信息")
    @Parameter(name = "productId", description = "商品ID", required = true)
    public Result<ReviewStatisticsVO> getReviewStatistics(@PathVariable String productId) {
        log.info("获取商品评价统计: {}", productId);
        ReviewStatisticsVO result = reviewService.getReviewStatistics(productId);
        return Result.success(result);
    }

    @PostMapping("/{id}/like")
    @Operation(summary = "点赞评价", description = "用户点赞评价")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth
    @Log(description = "点赞评价", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> likeReview(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 点赞评价: {}", userId, id);
        Boolean result = reviewService.likeReview(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}/like")
    @Operation(summary = "取消点赞评价", description = "用户取消点赞评价")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth
    @Log(description = "取消点赞评价", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> unlikeReview(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 取消点赞评价: {}", userId, id);
        Boolean result = reviewService.unlikeReview(id, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/reply")
    @Operation(summary = "回复评价", description = "回复评价")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth
    @Log(description = "回复评价", operationType = OperationTypeEnum.ADD)
    public Result<Boolean> replyReview(@PathVariable String id, @RequestParam String replyContent) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 回复评价: {}", userId, id);
        Boolean result = reviewService.replyReview(id, replyContent, userId);
        return Result.success(result);
    }

    @GetMapping("/hot/{productId}")
    @Operation(summary = "获取热门评价", description = "获取商品的热门评价")
    @Parameter(name = "productId", description = "商品ID", required = true)
    public Result<List<ProductReviewVO>> getHotReviews(@PathVariable String productId,
                                                     @RequestParam(defaultValue = "5") Integer limit) {
        log.info("获取热门评价: {}, 限制数量: {}", productId, limit);
        List<ProductReviewVO> result = reviewService.getHotReviews(productId, limit);
        return Result.success(result);
    }

    @PutMapping("/{id}/audit")
    @Operation(summary = "审核评价", description = "审核评价")
    @Parameter(name = "id", description = "评价ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "审核评价", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> auditReview(@PathVariable String id, 
                                     @RequestParam Integer status,
                                     @RequestParam(required = false) String auditRemark) {
        log.info("审核评价: {} -> {}", id, status);
        Boolean result = reviewService.auditReview(id, status, auditRemark);
        return Result.success(result);
    }

    @GetMapping("/pending-audit-count")
    @Operation(summary = "获取待审核评价数量", description = "获取待审核的评价数量")
    @RequireAuth(admin = true)
    public Result<Long> getPendingAuditCount() {
        log.info("获取待审核评价数量");
        Long result = reviewService.getPendingAuditCount();
        return Result.success(result);
    }
}
